/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="用户名：">
        <el-input size="small" v-model="formInline.nick" placeholder="请输入用户名称"></el-input>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" size="middle" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60" >
      </el-table-column>
      <el-table-column  prop="nick" label="用户名称"  align="center">
      </el-table-column>
      <el-table-column  prop="id" label="用户ID" align="center">
      </el-table-column>
      <el-table-column  prop="role" label="角色" align="center">
        <template slot-scope="scope">
          <el-tag type="">{{scope.row.role=='admin'?'管理员':'普通用户'}}</el-tag>
         </template>

      </el-table-column>
      <el-table-column  prop="phone" label="电话" align="center">
      </el-table-column>
      <el-table-column align="center" label="操作" >
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->


    <el-drawer
  title="编辑"
  :visible.sync="editFormVisible"
  :direction="direction"
  >
  <el-form :model="FormInfo" :rules="rulesFormEdit" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名称" prop="name">
    <el-input v-model="FormInfo.name" style="width:80%"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input type="password" v-model="FormInfo.password" style="width:80%"></el-input>
  </el-form-item>
  <el-form-item label="电话" prop="phone">
    <el-input type="phone" v-model="FormInfo.phone" style="width:80%"></el-input>
  </el-form-item>
  <el-form-item label="角色" prop="role">
    <el-select v-model="FormInfo.role" placeholder="请选择角色" style="width:80%">
      <el-option label="管理员" value="admin"></el-option>
      <el-option label="普通用户" value="user"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">确定修改</el-button>
   
  </el-form-item>
</el-form>
</el-drawer>
  </div>

 
</template>

<script>
import { deptList, deptSave, deptDelete } from '../../api/userMG'
import Pagination from '../../components/Pagination'
import { getUserList,removeUser,updateUser } from '@/api'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      FormInfo: {
          name: '',
          password: '',
          role: '',
          phone: '',
           },
      editForm: {
        deptId: '',
        deptName: '',
        deptNo: '',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
      rulesFormEdit: {
            name: [
            { required: true, message: '请输入名称', trigger: 'blur' },
            { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur',
            min: 6, max: 8, message: '长度在 6 到 8 个字符', }
          ],
          role: [
            { required: true, message: '请选择角色', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '请输入电话', trigger: 'blur',
            min: 11, max: 11, message: '请输入正确电话号码', trigger: 'blur' }
          ]},
      formInline: {
        current: 1,
        size: 10,
        total: 10,
        nick:''

       
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
      
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getData(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取公司列表
    getData(parameter) {
      this.loading = true
      // 模拟数据开始
   
      getUserList(parameter).then(res=>{
        // console.log(res.result)
        this.loading = false
      this.listData = res.result.records
      this.formInline.current = res.result.current
      this.formInline.size =res.result.size
      this.formInline.total = res.result.total
      })
     
     
      // 模拟数据结束

      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      // deptList(parameter)
      //   .then(res => {
      //     this.loading = false
      //     if (res.success == false) {
      //       this.$message({
      //         type: 'info',
      //         message: res.msg
      //       })
      //     } else {
      //       this.listData = res.data
      //       // 分页赋值
      //       this.pageparm.currentPage = this.formInline.page
      //       this.pageparm.pageSize = this.formInline.limit
      //       this.pageparm.total = res.count
      //     }
      //   })
      //   .catch(err => {
      //     this.loading = false
      //     this.$message.error('菜单加载失败，请稍后再试！')
      //   })
    },
    // 分页插件事件
    callFather(parm) {
      
  
      this.formInline.size = parm.pageSize
      this.getData(this.formInline)
    },
    // 搜索事件
    search() {
      this.getData(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      console.log(row)
      if (row != undefined && row != 'undefined') {
       
        this.FormInfo.role = row.role
        this.FormInfo.nick = row.nick
        this.FormInfo.phone = row.phone
        this.FormInfo.password = row.password
        this.FormInfo.id= row.id
      } 
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          let data={
              id:this.FormInfo.id,
              nick:this.FormInfo.name,
             password:this.FormInfo.password,
             phone:this.FormInfo.phone,
             role:this.FormInfo.role
            }
          console.log(this.FormInfo)
          updateUser(data).then(res=>{
              console.log(res)
              if(res.code ==200){
                this.$message.success('修改成功')
                this.editFormVisible = false
                this.getData(this.formInline)
              }else{
                this.$message.error('修改失败')
              }
            })
        } else {
          return false
        }
      })
    },
    // 删除用户
    deleteUser(index, row) {
      this.$confirm('确定要删除该用户信息吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // let data={
          //   idList:[row.id]
          // }
          removeUser(row.id).then(res=>{
            if(res.code ==200){
              this.$message({
                  type: 'success',
                  message: '用户已删除!'
                })
                this.getData(this.formInline)
            }else{
              this.$message({
                  type: 'error',
                  message: '用户删除失败!'
                })
            }
          })
      
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    },
    
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 